package com.jackstraw.jack_base.datasource.redis.factory.impl;

import com.jackstraw.jack_base.datasource.common.factory.DataSourceBuildFactory;
import com.jackstraw.jack_base.datasource.redis.bean.RedisDataSourceTemplate;
import com.jackstraw.jack_base.datasource.redis.bean.RedisPropertiesEntity;
import com.jackstraw.jack_base.datasource.redis.util.RedisConnectionUtil;
import com.jackstraw.jack_base.util.SpringUtil;
import io.lettuce.core.resource.ClientResources;
import org.redisson.api.RedissonClient;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;


/**
 * redis服务类工厂
 */
public class RedisBuildFactory implements DataSourceBuildFactory<RedisDataSourceTemplate, RedisPropertiesEntity> {

    @Override
    public RedisDataSourceTemplate build(String tenantName, String dataSourceName, RedisPropertiesEntity dataSourceEntity) {

        ClientResources clientResources = RedisConnectionUtil.clientResources();
        LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer = SpringUtil.getBean(LettuceClientConfigurationBuilderCustomizer.class);

        LettuceClientConfiguration lettuceClientConfiguration
                = RedisConnectionUtil.lettuceClientConfiguration(dataSourceEntity,
                clientResources,
                dataSourceEntity.getGenericObjectPoolConfig(),
                lettuceClientConfigurationBuilderCustomizer);

        RedisCacheConfiguration redisCacheConfiguration = RedisConnectionUtil.redisCacheConfiguration(dataSourceEntity.getSeconds());

        RedisConfiguration redisConfiguration = RedisConnectionUtil.redisConfiguration(dataSourceEntity);

        RedisConnectionFactory redisConnectionFactory = RedisConnectionUtil.redisConnectionFactory(redisConfiguration, lettuceClientConfiguration);

        RedisCacheManager redisCacheManager = RedisConnectionUtil.redisCacheManager(redisConnectionFactory, redisCacheConfiguration);

        org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate = RedisConnectionUtil.redisTemplate(redisConnectionFactory);

        RedissonClient redisson = RedisConnectionUtil.redisson(dataSourceEntity);


        RedisDataSourceTemplate redisDataSourceTemplate = new RedisDataSourceTemplate();
        redisDataSourceTemplate.setRedisTemplate(redisTemplate);
        redisDataSourceTemplate.setRedissonClient(redisson);
        redisDataSourceTemplate.setCacheManager(redisCacheManager);
        redisDataSourceTemplate.setRedisCacheConfiguration(redisCacheConfiguration);
        redisDataSourceTemplate.setRedisConfiguration(redisConfiguration);
        redisDataSourceTemplate.setRedisConnectionFactory(redisConnectionFactory);
        redisDataSourceTemplate.setPropertiesEntity(dataSourceEntity);
        return redisDataSourceTemplate;
    }
}
